﻿/*
 * ColorSpaceConverter
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;


namespace Petra.Gis.ImagingServices.Tiff
{
  public static class ColorSpaceConverter
  {

    //private delegate byte[] GetPixel(TiffBitmapPage image);



    static ColorSpaceConverter()
    {

    }




    private static bool isColorSpaceConverterNeeded(Metadata metadata, PixelFormat destPixelFormat)
    {
      //todo: implement this !!
      return true;
    }





    public static byte[] Convert(BitmapBlock block, TiffPixelFormat tiffSrcPixelFormat, PixelFormat destPixelFormat)
    {

      if ((destPixelFormat != PixelFormats.Rgb24) || (destPixelFormat.BitsPerPixel < 8) || (destPixelFormat.BitsPerPixel % 8 != 0))
        throw new ArgumentException("unsupported bitsPerPixel, must be >= 8 and modulo 8");

      //if(!isColorSpaceConverterNeeded(metadata, destPixelFormat))
      //  return block.
      //design destination bitmap
      byte[] destBitmap = new byte[BitmapBlock.GetStrideLenght(block.Bounds.Width, destPixelFormat.BitsPerPixel) * block.Bounds.Height];

      if (tiffSrcPixelFormat == TiffPixelFormats.PlanarYCbCr24bpp)
      {
        //SeparatedBitmapBlock b = (SeparatedBitmapBlock)block;
        PCDYCbCrConversion.convertYccToRgb((SeparatedBitmapBlock)block, destBitmap);
      }

      return destBitmap;
    }


  }
}
